package com.example.blog2023.service.impl;

import com.example.blog2023.bean.dto.BlogsInputDto;
import com.example.blog2023.bean.pojo.Blog;
import com.example.blog2023.bean.vo.AdminBlogsVo;
import com.example.blog2023.bean.vo.GroupBlog;
import com.example.blog2023.mapper.BlogLabelMapper;
import com.example.blog2023.mapper.BlogMapper;
import com.example.blog2023.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;

/**
 * @author cfy
 * @date 2023/12/11
 **/
@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private BlogMapper blogMapper;
    @Autowired
    private BlogLabelMapper blogLabelMapper;

    @Override
    public ArrayList<AdminBlogsVo> getAllBlogs() {
        ArrayList<Blog> blogs = blogMapper.getBlogs();
        return getAdminBlogsVo(blogs);
    }

    @Override
    public ArrayList<AdminBlogsVo> getBlogsByCondition(String title, String gender, String recommend) {
        ArrayList<Blog> blogs = blogMapper.getBlogsByCondition(title,gender,recommend);
        return getAdminBlogsVo(blogs);
    }

    @Override
    public boolean deleteById(Integer id) {
        boolean success = false;
        int i = blogMapper.deleteById(id);
        if(i>=0){
            success = true;
        }
        return success;

    }

    @Override
    public boolean insertBolg(BlogsInputDto bolgsInputDto) {
        char[] a ={'0','0','0','0'};
        if (bolgsInputDto.getRecommend()!=null){
            a[0] = '1';
        }
        if (bolgsInputDto.getShareInfo()!=null){
            a[1] = '1';
        }
        if (bolgsInputDto.getAppreciation()!=null){
            a[2] = '1';
        }
        if (bolgsInputDto.getComment()!=null){
            a[3] = '1';
        }
        boolean success = false;
        Blog blog  = Blog.builder()
                .title(bolgsInputDto.getTitle())
                .type(bolgsInputDto.getType())
                .recommend(a[0])
                .content(bolgsInputDto.getContent())
                .shareInfo(a[1])
                .appreciation(a[2])
                .comment(a[3])
                .createTime(LocalDateTime.now())
                .format(bolgsInputDto.getFormat())
                .build();
        int i = blogMapper.insertBlog(blog);
        int k = 0;
        Blog b = blogMapper.getBlogByTitle(bolgsInputDto.getTitle());
        ArrayList<String> labels = bolgsInputDto.getLabel();
        for(String label:labels){
            k = blogLabelMapper.insertLabel(label, b.getId());
        }
        if (i>0&&k>0){
            success = true;
        }
        return success;
    }

    /**
     * 构建ArrayList<AdminBlogsVo>
     * @param blogs
     * @return
     */
    private ArrayList<AdminBlogsVo> getAdminBlogsVo(ArrayList<Blog> blogs){
        ArrayList<AdminBlogsVo> adminBlogsVos = new ArrayList<>();
        for(Blog b:blogs){
            char recommend = '否';
            if (b.getRecommend() == '1'){
                recommend = '是';
            }
            AdminBlogsVo adminBlogsVo = AdminBlogsVo.builder()
                    .id(b.getId())
                    .title(b.getTitle())
                    .createTime(b.getCreateTime())
                    .recommend(recommend)
                    .type(b.getType())
                    .build();
            adminBlogsVos.add(adminBlogsVo);
        }
        return adminBlogsVos;
    }
}
